package Action;

import java.util.UUID;

import org.apache.struts.actions.DispatchAction;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import Aptech.Common.Formater;
import Aptech.Service.SV_Custommers;
import Form.BuiltsForm;
import Form.CusForm;
import HibernateMappingPoJo.Builts;
import HibernateMappingPoJo.Custommers;

public class CusAction extends DispatchAction {

	private CusForm cusForm;
	private SV_Custommers mgCus = new SV_Custommers();

	public ActionForward getAllCustomer(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		cusForm = (CusForm) form;
		cusForm.setArrCus(mgCus.getAllCustomer());
		request.setAttribute("sitemap", "List of Custommer");
		return mapping.findForward("list");
	}

	public ActionForward addCustommer(ActionMapping map, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		cusForm = (CusForm) form;

		String type = request.getParameter("type");
		request.setAttribute("sitemap", "Add A New Custommer");
		if (type == null) {
			request.setAttribute("type", "add");
			return map.findForward("edit");
		} else {
			UUID id = UUID.randomUUID();
			Custommers cus = new Custommers();
			cus.setId(id.toString());
			cus.setName(cusForm.getName());
			cus.setAdress(cusForm.getAdress());
			cus.setEmail(cusForm.getEmail());
			cus.setNote(cusForm.getNote());
			cus.setPhoneNum(cusForm.getPhoneNum());

			mgCus.addCustommer(cus);
			request.setAttribute("save", "save");
		}

		return map.findForward("edit");

	}

	public ActionForward deleteCustommer(ActionMapping map, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("id");
		mgCus.deleteCustommer(id);
		response.sendRedirect("/NTB/cus.do?method=getAllCustomer");

		return map.findForward("list");

	}

	public ActionForward editCustommer(ActionMapping map, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		cusForm = (CusForm) form;
    	String id = request.getParameter("id");    	
    	String type = request.getParameter("type");
    	request.setAttribute("sitemap", "Edit Information of A Custommer");
    	
    	if (type.equals("edit1")) {
            request.setAttribute("id", id);
            request.setAttribute("type", type);
           
            // Set value for Form
            Custommers cus = mgCus.getCustommerByID(id);
            if(cus != null){
            	cusForm.setId(id);
                cusForm.setName(cus.getName());
                cusForm.setAdress(cus.getAdress());
                cusForm.setEmail(cus.getEmail());
                cusForm.setPhoneNum(cus.getPhoneNum());
                cusForm.setNote(cus.getNote());
            }
                      
        } else {        
        	Custommers cus = new Custommers();
			cus.setId(id.toString());
			cus.setName(cusForm.getName());
			cus.setAdress(cusForm.getAdress());
			cus.setEmail(cusForm.getEmail());
			cus.setNote(cusForm.getNote());
			cus.setPhoneNum(cusForm.getPhoneNum());          
			mgCus.editCustommer(cus);
			request.setAttribute("save", "save");
        }


        return map.findForward("edit");

	}

	public ActionForward detailCustommer(ActionMapping map,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		cusForm = (CusForm) form;
        String id = request.getParameter("id");
        request.setAttribute("id", id);
        request.setAttribute("type", "detail");
        request.setAttribute("sitemap", "Information Detail of A Custommer");
        
            // Set value for Formpe
        Custommers cus = mgCus.getCustommerByID(id);
        if(cus != null){
        	cusForm.setId(id);
            cusForm.setName(cus.getName());
            cusForm.setAdress(cus.getAdress());
            cusForm.setEmail(cus.getEmail());
            cusForm.setPhoneNum(cus.getPhoneNum());
            cusForm.setNote(cus.getNote());
        }
            
    	return map.findForward("edit");

	}
	
	public ActionForward findCustommer(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		cusForm = (CusForm) form;
		cusForm.setArrCus(mgCus.getAllCustomer());

		return mapping.findForward("listfind");
	}

}
